<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$this->add_member();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->plugin_setting('basic');
$basic['topfeeday']=$basic['topfeeday']?:1;
$basic['topfeeyue']=$basic['topfeeyue']?:10;
$basic['topfeeji']=$basic['topfeeji']?:20;
$return['setting']=$basic;
if($_RQ['op']=='main'){
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['gamexieyi']=$this->plugin_setting('gamexieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['checked']){
			exi('请先阅读《会员协议》','error');
		}
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		if($packname==1){
			$fee=$basic['topfeeday']*$packnum;
			$packtitle='天';
		}elseif($packname==2){
			$fee=$basic['topfeeyue']*$packnum;
			$packtitle='月';
		}elseif($packname==3){
			$fee=$basic['topfeeji']*$packnum;
			$packtitle='季';
		}
		$item=pdo_get('jy_sort_item',array('id'=>$postdata['pid']));
		$title='置顶帖子收费：'.$packnum.'《'.$packtitle.'》';
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packname'=>$packname,
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_sort_fabulog','ordersn'),
			'type'=>$item['type'],
			'title'=>$title,
			'itemid'=>$postdata['pid'],
			'fee'=>$fee,
			'createtime'=>TIMESTAMP,
			'ispay'=>0
		);
		pdo_insert('jy_sort_fabulog',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']=$title;
		$params['id']=$id;
		$params['plugin']='jy_sort';
	//	$params['pay_type']='wechat';//可选，此项作用为指定支付方式
	//	$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_sort/buytop/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$post['pid']=intval($_RQ['pid']);
	$return['post']=$post;
	$return['fee']=price($basic['topfeeday']);
	$return['fee0']=price($basic['topfeeday']*1.5);
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_sort_fabulog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_sort_fabulog',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$item=pdo_get('jy_sort_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['itemid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*120;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*30;
	}
	if($item['topendtime']<TIMESTAMP){		
		$topendtime=TIMESTAMP+$order['packnum']*24*3600;
	}else{
		$topendtime =$item['topendtime']+$order['packnum']*24*3600;
	}
	pdo_update('jy_sort_item',array('ishot'=>1,'topendtime'=>$topendtime),array('id'=>$order['itemid'],'uniacid'=>$_SESSION['uniacid']));
	//发送模板消息
	//$this->notice('buyvip',$_SESSION['uid'],'',$order['id']);
	exi('支付成功','success','jy_sort/buytop/');
}
